package com.reservation.service.impl;

import com.reservation.entity.User;
import com.reservation.mapper.UserMapper;
import com.reservation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        if (checkUsername(user.getUsername()) || checkStudentId(user.getStudentId())) {
            return false;
        }
        user.setRole("student");
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean checkUsername(String username) {
        return userMapper.findByUsername(username) != null;
    }

    @Override
    public boolean checkStudentId(String studentId) {
        return userMapper.findByStudentId(studentId) != null;
    }
} 